package ru.iasokolov.demo.entity

import org.springframework.stereotype.Service
import java.util.*
import kotlin.jvm.optionals.getOrNull

@Service
abstract class AbstractBaseServiceImpl<Entity : AbstractBaseEntity>(
    private val abstractBaseRepository: AbstractBaseRepository<Entity>
): AbstractBaseService<Entity> {

    override fun save(entity: Entity): Entity {
        return abstractBaseRepository.save<Entity>(entity)
    }

    override fun findAll(): List<Entity> {
        return abstractBaseRepository.findAll()
    }

    override fun findById(entityId: UUID): Entity? {
        return abstractBaseRepository.findById(entityId).getOrNull()
    }

    override fun update(entity: Entity): Entity {
        return abstractBaseRepository.save<Entity>(entity)
    }

    override fun updateById(entity: Entity, entityId: UUID): Entity? {
        return findById(entityId)?.let { abstractBaseRepository.save<Entity>(entity)}
    }

    override fun delete(entity: Entity) {
        abstractBaseRepository.delete(entity)
    }

    override fun deleteById(entityId: UUID) {
        abstractBaseRepository.deleteById(entityId)
    }
}